package com.changpei.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changpei.common.result.Result;
import com.changpei.core.pojo.Content;
import com.changpei.core.service.ContentService;
import com.changpei.core.utils.SearchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Title : Content</p>
 * <p>Description : content的控制层</p>
 *
 * @author weibiao
 * @date 2020年3月7日
 */

@RestController
@RequestMapping("/content")
@CrossOrigin
public class ContentController {

    @Autowired
    private ContentService contentService;

    /**
     * <p>Title : add</p>
     * <p>Description : 新增Content数据</p>
     *
     * @param content
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Long> add(@RequestBody Content content) {
        // 调用ContentService实现添加Content
        if (contentService.save(content)) {
            return Result.ok(content.getId());
        }
        return Result.failed();
    }

    /**
     * <p>Title : delete</p>
     * <p>Description : Content根据ID数据</p>
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable Long id) {
        // 调用ContentService实现根据主键删除
        if (contentService.removeById(id)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : update</p>
     * <p>Description : Content根据ID修改</p>
     *
     * @param content
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody Content content, @PathVariable Long id) {
        // 设置主键值
        content.setId(id);
        // 调用ContentService实现修改Content
        if (contentService.updateById(content)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : Content分页搜索实现</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<Content>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用ContentService实现分页查询Content
        Page<Content> pageInfo = contentService.page(new Page<>(pageNum, pageSize));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : Content分页条件搜索实现</p>
     *
     * @param content
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<Content>> findPage(@RequestBody(required = false) Content content, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用ContentService实现分页条件查询Content
        Page<Content> pageInfo = contentService.page(new Page<>(pageNum, pageSize), SearchUtil.initQuery(content));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findList</p>
     * <p>Description : 多条件搜索Content数据</p>
     *
     * @param content
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Content>> findList(@RequestBody(required = false) Content content) {
        // 调用ContentService实现条件查询Content
        List<Content> list = contentService.list(SearchUtil.initQuery(content));
        return Result.ok(list);
    }

    /**
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询Content数据</p>
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<Content> findById(@PathVariable Long id) {
        // 调用ContentService实现根据主键查询Content
        Content content = contentService.getById(id);
        return Result.ok(content);
    }

    /**
     * <p>Title : findOne</p>
     * <p>Description : 多条件搜索Content数据，结果返回一条</p>
     *
     * @param content
     * @return
     */
    @PostMapping(value = "/findOne")
    public Result<Content> findOne(@RequestBody(required = false) Content content) {
        // 调用ContentService实现条件查询Content
        Content contentResult = contentService.getOne(SearchUtil.initQuery(content));
        return Result.ok(contentResult);
    }

    /**
     * <p>Title : findAll</p>
     * <p>Description : 查询Content全部数据</p>
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result<List<Content>> findAll() {
        // 调用ContentService实现查询所有Content
        List<Content> list = contentService.list();
        return Result.ok(list);
    }

    /**
     * 从id位置查询
     * @param id
     * @return
     */
    @GetMapping(value = "/findBaseId/{id}")
    public Result<List<Content>> findBaseId(@PathVariable Long id) {
        // 调用UserService实现查询所有User
        QueryWrapper<Content> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id", id);
        List<Content> list = contentService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     *
     * <p>Title : add</p>
     * <p>Description : 新增Content数据</p>
     * @param contents
     * @return
     */
    @PostMapping(value = "/addAll")
    public Result<Boolean> addAll(@RequestBody List<Content> contents) {
        return Result.ok(contentService.saveBatch(contents));
    }
}
